<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { getShareAPI, uploadAPI } from '@/apis/shareAPI'
// 导入时间格式化函数
import { changeTime } from '@/composables/getTime'
// 导入朋友圈图片上传功能
import type { UploadFile } from 'element-plus'
// 编辑朋友圈，发朋友圈
const drawer = ref(false)
const dialogImageUrl = ref('')


const dialogVisible = ref(false)
const disabled = ref(false)
// 删除上传图片
const handleRemove = (file: UploadFile) => {
  console.log(file)
}
// 放大图片
const handlePictureCardPreview = (file: UploadFile) => {
  dialogImageUrl.value = file.url!
  dialogVisible.value = true
}
// 下载图片
const handleDownload = (file: UploadFile) => {
  console.log(file)
}
// 文本域
const textarea = ref('')

// 发送朋友圈文本和图片
const submit = async () => {
  const shareText = textarea.value
  const photo = dialogImageUrl.value

  await uploadAPI({ shareText, photo })
}
// onMounted(() => submit())

// 获取API
const friendList = ref([])
const getFriend = async () => {
  const res = await getShareAPI()
  console.log(res);
  friendList.value = res.data
}

onMounted(() => getFriend())

</script>
<template>
  <!-- 朋友圈背景框 -->
  <el-affix :offset="0" class="friend-bg">
    <div>朋友圈</div>
    <!-- 发朋友圈 -->
    <el-icon class="friend-add" @click="drawer = true">
      <Camera />
    </el-icon>
    <el-drawer v-model="drawer" title="I am the title" :with-header="false">
      <span>这一刻的想法...</span>
      <!-- 输入文本域 -->
      <el-input v-model="textarea" maxlength="500" placeholder="请输入..." show-word-limit type="textarea" rows="8" />
      <!-- 上传图片 -->
      <el-upload action="#" list-type="picture-card" :auto-upload="false">
        <el-icon>
          <Plus />
        </el-icon>

        <template #file="{ file }">
          <div>
            <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
            <span class="el-upload-list__item-actions">
              <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                <el-icon><zoom-in /></el-icon>
              </span>
              <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
                <el-icon>
                  <Download />
                </el-icon>
              </span>
              <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                <el-icon>
                  <Delete />
                </el-icon>
              </span>
            </span>
          </div>
        </template>
      </el-upload>

      <el-dialog v-model="dialogVisible">
        <img w-full :src="dialogImageUrl" alt="Preview Image" />
      </el-dialog>
      <el-button @click="submit">发表</el-button>
    </el-drawer>
  </el-affix>
  <ul infinite-scroll-immediate="true" class="infinite-list" style="overflow: auto; ">
    <li v-for="i in friendList" :key="i.id" class="infinite-list-item">
      <!-- 头像 -->
      <div class="demo-fit">
        <div class="block">
          <el-avatar shape="square" :size="80" :src="i.head" />
        </div>
        <h1>{{ i.name }}</h1>
        <div class="content">
          {{ i.shareText }}
          <div class="time">{{ changeTime(i.uplodeTime).gshTime }}</div>
        </div>
      </div>
    </li>
  </ul>
</template>


<style scoped lang="scss">
// 隐藏滚动条
::-webkit-scrollbar {
  display: none;
}

.friend-bg {
  position: relative;
  width: 100%;
  height: 4vw;
  background-color: #fff;
  line-height: 4vw;
  font-size: 2vw;
  text-align: center;

  .friend-add {
    position: absolute;
    right: 2vw;
    top: 1vw;
  }
}

.infinite-list {
  position: relative;
  left: 12vw;
  width: 70vw;
  height: 100vh;
  padding: 0;
  margin: 0;
  list-style: none;

  .infinite-list-item {
    position: relative;
    height: 20vw;
    color: var(--el-color-primary);
    background-color: pink;
    margin: 10px;

    // 头像
    .demo-fit {
      position: absolute;
      left: 2vw;
      top: 2vw;

      .block {
        float: left;
      }

      h1 {
        margin-left: 5vw;
      }

      .content {
        position: relative;
        float: left;
        width: 20vw;
        font-size: 2vw;
        color: #000;

        .time {
          position: absolute;
          bottom: -1.5vw;
          font-size: .8vw !important;
        }
      }

    }
  }
}
</style>
